//
//  InvitationDetailViewController.m
//  XiangMuB
//
//  Created by 向修根 on 16/8/22.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "InvitationDetailViewController.h"
#import "GetDataHandle.h"
#import "InvitationDetailsTableViewCell.h"
#import "invitationDetailModel.h"
#import <MJRefresh.h>

@interface InvitationDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *InvitationTableView;

@property (strong,nonatomic) NSMutableArray *dataArray;

@property (assign,nonatomic) NSInteger currentPage;

@end

@implementation InvitationDetailViewController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.InvitationTableView.delegate =self;
    self.InvitationTableView.dataSource = self;
    
    [self.InvitationTableView registerNib:[UINib nibWithNibName:@"InvitationDetailsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];

    
    //    下拉加载
    self.InvitationTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        [self getSession];
    }];
    [self.InvitationTableView.mj_header beginRefreshing];
    [self.InvitationTableView reloadData];

    //    上拉刷新
    self.InvitationTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self DropgetSession];
    }];

    
//    取消tableview 中的换行线
    self.InvitationTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // Do any additional setup after loading the view from its nib.
}

//数据解析
- (void)getSession{
    NSString *urlStr = [NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/hot/sports2_bbs/%@/0/20/10/2/2",self.urlID];
[[GetDataHandle sharedDataHandle]getDataWithUrlString:urlStr withGetDataBlock:^(NSData *data) {
    NSDictionary *di = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSArray *array = di[@"hotPosts"];
    for (NSDictionary *dic in array) {
        NSDictionary *dict = dic[@"1"];
        invitationDetailModel *model = [invitationDetailModel new];
        [model setValuesForKeysWithDictionary:dict];
        [self.dataArray addObject:model];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.InvitationTableView.mj_header endRefreshing];
        [self.InvitationTableView reloadData];
    });
    
}];
}

- (void)DropgetSession{
    self.currentPage += 20;
    NSString *urlStr = [NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/hot/sports2_bbs/%@/%ld/20/10/2/2",self.urlID,self.currentPage];
    [[GetDataHandle sharedDataHandle]getDataWithUrlString:urlStr withGetDataBlock:^(NSData *data) {
        NSDictionary *di = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSArray *array = di[@"hotPosts"];
        for (NSDictionary *dic in array) {
            NSDictionary *dict = dic[@"1"];
            invitationDetailModel *model = [invitationDetailModel new];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.InvitationTableView.mj_footer endRefreshing];
            [self.InvitationTableView reloadData];
        });
        
    }];
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InvitationDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    invitationDetailModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    //        点击cell不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGFloat height = [self textHeight:model.b];
    cell.b.frame = CGRectMake(cell.b.frame.origin.x, cell.b.frame.origin.y, cell.b.frame.size.width, height);
    return cell;

}

//计数字符串所占矩形区域的大小，并返回高度
- (CGFloat)textHeight:(NSString *)test{
    //    计算字符串所占的大小
    CGRect rect = [test boundingRectWithSize:CGSizeMake(394, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    return rect.size.height;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    invitationDetailModel *model = self.dataArray[indexPath.row];
    CGFloat height = [self textHeight:model.b];
    return height + 100;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
